﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manageweb_Stock_StockPurchase : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"]==null)
        {
            Response.Redirect("~/MemberLogin.aspx");
        }

        if (!Page.IsPostBack)
        {
            BindStockInfo();
        }
    }

    protected void BindStockInfo()
    {
        SQLExpressDAL.StockTableAdapters.StockTableAdapter adp = new SQLExpressDAL.StockTableAdapters.StockTableAdapter();

        this.GV_StockInfoList.DataSource = adp.GetData();
        this.GV_StockInfoList.DataBind();

    }

   

    protected void btnOK_Click(object sender, EventArgs e)
    {

        InsertBuyRecord();

    }

    SQLExpressDAL.StockTableAdapters.RecordTableAdapter recordAdp = new SQLExpressDAL.StockTableAdapters.RecordTableAdapter();
    SQLExpressDAL.Stock.RecordDataTable recordTab;

    protected void InsertBuyRecord()
    {
         
        String msg = "";
        String JYType = Request["hidJYType"];
        String StockFrom = "admin";

        try
        {
            String stockCode = Request["txtStockCode"];
            String stockName = Request["txtStockName"];
            Decimal stockPrice = Convert.ToDecimal(Request["txtPrice"]);
            Int32 stockCount = Convert.ToInt32(this.txtCount.Text.Trim());
            Decimal stockCost = stockPrice * stockCount;


            //检查股票数量数量
            SQLExpressDAL.StockTableAdapters.StockTableAdapter stockAdp = new SQLExpressDAL.StockTableAdapters.StockTableAdapter();
            SQLExpressDAL.Stock.StockDataTable tab = stockAdp.GetStockCountByStockCode(stockCode);
            Int32 stockCurrCount = Convert.ToInt32(tab.Rows[0]["count"]);
           
            //检查账户金额
            SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
            SQLExpressDAL.Users.UsersDataTable userTab = userAdp.CheckVCurrency(Session["userID"].ToString(), stockCost);
            int isContinue = userTab.Rows.Count;

            if (isContinue == 0)
            {
                msg = "账户金额不足";
                return;
            }
            //购买来自大盘 还是会员
            if (Request["hidJYType"].Equals("member"))
            {
                StockFrom = Request["hidMemID"];

                if (stockCurrCount != 0)
                {
                    msg = "必须先从大盘中购买，当大盘中数量为零时,才可从会员购买.";
                    return;
                }
                else
                {

                    recordTab = recordAdp.GetSingleRecord(Convert.ToInt32(Request["hidRecordID"]));
                    int memRecordCount = Convert.ToInt32(recordTab.Rows[0]["count"]);

                    if (stockCount > memRecordCount)
                    {
                        msg = "购买数量超过总数量";
                        return;
                    }
                }

            }
            else
            {
                if (stockCount > stockCurrCount)
                {
                    msg = String.Format("购买数量超过大盘数量,最多购买{0}股;其它数量请从会员股票中购买.", stockCurrCount);
                    //stockCount = stockCurrCount;
                    return;
                }
            }

            int flag = recordAdp.InsertBuyRecord(Session["UserID"].ToString(), stockCode, stockName, stockPrice, stockCount, "IN", null, StockFrom,1);

            if (flag > 0)
            {
                if (Request["hidJYType"].Equals("member"))
                {   //更新record的状态、数量 已被购买
                    recordAdp.UpdateRecordState(stockCount,Convert.ToInt32(Request["hidRecordID"]));

                }
                else
                {
                    stockAdp.UpdateStockCount(stockCount, stockCode);
                }

                userAdp.UpdateVCurrencyDiscount(stockCost, Session["UserID"].ToString());

                msg = "购买成功";
            }

        }
        catch (Exception ex)
        {
            msg = "购买失败,请联系客服.";

        }
        finally
        {
            ClearTxt();
            this.lblMsg.Text = msg;
        }

        BindStockInfo();

    }



    protected void ClearTxt()
    {
        this.txtCount.Text = String.Empty;
    }

    protected void GV_StockInfoList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GV_StockInfoList.PageIndex = e.NewPageIndex;
        this.GV_StockInfoList.DataBind();
    }

    protected void btnBindMemStock_Click(object sender, EventArgs e)
    {
        SQLExpressDAL.StockTableAdapters.RecordTableAdapter adp = new SQLExpressDAL.StockTableAdapters.RecordTableAdapter();
        this.GV_MemStock.DataSource = adp.GetMemSaleStock(this.hidMemStockCode.Value);
        this.GV_MemStock.DataBind();
    }   

    protected void GV_MemStock_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        String strFormat = "'{0}','{1}','{2}','{3}','{4}','{5}'";
        if (e.Row.Cells.Count > 2)
        {
            e.Row.Cells[5].Style.Add(HtmlTextWriterStyle.Display, "none");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            strFormat = String.Format(strFormat, e.Row.Cells[0].Text, e.Row.Cells[1].Text, e.Row.Cells[2].Text, e.Row.Cells[3].Text, e.Row.Cells[4].Text, e.Row.Cells[5].Text);
            e.Row.Attributes.Add("onclick", "test(" + strFormat + ")");

        }
    }
}
